﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyBank
{
    public class Card:Bank_Card
    {

        public Card(int card_ID, int Card_num, string Account_Name, double Card_Balance, string Card_Tel, string Card_Name) : base(card_ID, "0", "0", 0, Card_Name)
        {
            this.Card_ID = card_ID;
            this.Card_num = Card_num;
            this.Card_Balance = Card_Balance;
            this.Account_Name = Account_Name;
            this.Card_Tel = Card_Tel;
            this.Card_Name = Card_Name;
        }

        public new int Card_ID = 0;
        private int Card_num;    // 卡号
        private string Card_Tel;    // 绑定手机号
        private string Account_Name;   // 持卡人姓名

        public double Card_Balance; // 卡内余额


        // 存入银行卡
        public bool SaveMoney(double money)
        {
            if (this.Card_Balance < 0) return false;  // 卫语句（存入金额不能为负）
            this.Card_Balance += money;

            SQLiteDataReader reader = CurrentAccount.sql.ReadFullTable("AllCards");
            MessageBox.Show(this.Card_num.ToString());
            while (reader.Read())
            {
                CurrentAccount.sql.Log(DateTime.Now.ToString(), CurrentAccount.account_ID, "存入", money, this.Card_ID);
                double oldMoney = reader.GetDouble(reader.GetOrdinal("Card_Balance"));
                CurrentAccount.sql.UpdateValues("AllCards", new string[] { "Card_Balance" }, new string[] { (oldMoney + money).ToString() }, "Card_num", this.Card_num.ToString());
                return true;
            }

            return false;


        }

        public bool WithDrawMoney(double money)
        {
            if (money > this.Card_Balance) return false;  //取出金额要小于等于余额
            this.Card_Balance -= money;
            SQLiteDataReader reader = CurrentAccount.sql.ReadFullTable("AllCards");

            while (reader.Read())
            {
                CurrentAccount.sql.Log(DateTime.Now.ToString(), CurrentAccount.account_ID, "取出", money, this.Card_ID);
                double oldMoney = reader.GetDouble(reader.GetOrdinal("Card_Balance"));
                CurrentAccount.sql.UpdateValues("AllCards", new string[] { "Card_Balance" }, new string[]{ (oldMoney - money).ToString()}, "Card_num", this.Card_num.ToString());
                return true;
            }

            return true;

        }
    }
}
